package com.example.wangning.mycarofcountry.find;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.findcar.brand.FindHotCar;
import com.example.wangning.mycarofcountry.findcar.brand.FindHotCarHotseries;
import com.example.wangning.mycarofcountry.findcar.brand.FindHotCarList;
import com.example.wangning.mycarofcountry.findcar.brand.FindHotCarResult;
import com.example.wangning.mycarofcountry.tool.DividerGridItemDecoration;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 16/1/22.
 */
public class FindFragment extends Fragment {
    private RecyclerView find_recyclerView;
    private RecyclerView find_Rv_tejia;
    private FindRecyclerViewAdapter frvAdapter;
    private FindTeJiaAdapter ftjAdapter;
    private VolleySingleton volleySingleton;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        find_recyclerView = (RecyclerView) view.findViewById(R.id.find_recyclerView);
        find_Rv_tejia = (RecyclerView) view.findViewById(R.id.find_Rv_tejia);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        find_recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        find_recyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        frvAdapter = new FindRecyclerViewAdapter(getActivity());
        find_recyclerView.setAdapter(frvAdapter);
        RecyclerViewHeader header = (RecyclerViewHeader) getActivity().findViewById(R.id.header);
        header.attachTo(find_recyclerView, true);
        find_Rv_tejia.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        ftjAdapter = new FindTeJiaAdapter(getActivity());
        find_Rv_tejia.setAdapter(ftjAdapter);

        initData();
        initDatas();
    }

    private void initData() {
        String url = "http://app.api.autohome.com.cn/autov5.0.0/Dealer/hotsaleseries-pm2-st1.json";
        volleySingleton = VolleySingleton.getVolleyIntance();
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FindHotCar fc = gson.fromJson(response, FindHotCar.class);
                if (fc != null) {
                    FindHotCarResult carResult = fc.getResult();
                    List<FindHotCarList> carLists = carResult.getList();
                    if (carLists != null && carLists.size() > 0) {
                        for (int i = 0; i < carLists.size(); i++) {
                            List<FindHotCarHotseries> carHotseries = carLists.get(i).getHotseries();
                            if (carHotseries != null && carHotseries.size() > 0) {
                                ftjAdapter.addData(carHotseries);
                            }
                        }
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);

    }
    private void initDatas() {
        String stringUrl = "http://app.api.autohome.com.cn/autov5.0.0/Dealer/hotsaleseries-pm2-st2.json";
        volleySingleton = VolleySingleton.getVolleyIntance();
        StringRequest request = new StringRequest(stringUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FindHotCar fc = gson.fromJson(response, FindHotCar.class);
                if (fc != null) {
                    FindHotCarResult carResult = fc.getResult();
                    List<FindHotCarList> carLists = carResult.getList();
                    if (carLists != null && carLists.size() > 0) {
                        for (int i = 0; i < carLists.size(); i++) {
                            List<FindHotCarHotseries> carHotseries = carLists.get(i).getHotseries();
                            if (carHotseries != null && carHotseries.size() > 0) {
                                frvAdapter.addData(carHotseries);
                            }
                        }
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);

    }

}
